package gewu.suleju.controller;

import gewu.suleju.model.pojo.HouseCollection;
import gewu.suleju.model.pojo.HouseInfor;
import gewu.suleju.model.pojo.HousePic;
import gewu.suleju.model.vo.Result;

import gewu.suleju.sevice.imp.HouseCollectionServiceImp;
import gewu.suleju.sevice.imp.HouseCollectionServiceImptjj;
import gewu.suleju.sevice.imp.HouseInforServiceImp;
import gewu.suleju.sevice.imp.HousePicServiceImp;
import gewu.suleju.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("HouseCollection")

public class MyCollectionController {
    @Autowired
    private HouseCollectionServiceImptjj houseCollectionServiceImp;

    @ResponseBody
    @PostMapping("findHouseCollectionsByUserId")
    public Result findHouseCollectionsByUserId(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer userid=(Integer)claims.get("id");
        List<HouseCollection> houseCollectionList=houseCollectionServiceImp.findHouseCollectionsByUserId(userid);
        return new Result("200","成功",houseCollectionList);
    }
    @Autowired
    private HouseInforServiceImp houseInforServiceImp;
    @Autowired
    private HousePicServiceImp  housePicServiceImp;
    @ResponseBody
    @PostMapping("findHouseInforsByIdIn")
    public Result findHouseInforsByIdIn(HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer userid=(Integer)claims.get("id");
        List<HouseCollection> houseCollectionList=new ArrayList();
        houseCollectionList=houseCollectionServiceImp.findHouseCollectionsByUserId(userid);
        List<Integer>  houseIdList=new ArrayList();
        for (int i = 0; i < houseCollectionList.size(); i++) {
            houseIdList.add(i, houseCollectionList.get(i).getHouseInforId());
        }

        HashMap<Integer,Object>  picMap=new HashMap();



        for(int i=0;i<houseIdList.size();i++){
            List<HousePic> housePicList=housePicServiceImp.findHousePicsByHouseInforId(houseIdList.get(i));




            picMap.put(i,housePicList);


        }
        HashMap<Object,Object> houseinfor = new HashMap<Object, Object>();
        List<HouseInfor> houseInforList=new ArrayList();
        houseInforList=houseInforServiceImp.findHouseInforsByIdIn(houseIdList);
        houseinfor.put("房子信息",houseInforList);
        houseinfor.put("房子图片",picMap);

        return  new Result("200","成功",houseinfor);

    }
}



